package com.iedu.demo.lgq.controller;

import cn.dev33.satoken.util.SaResult;
import com.iedu.demo.lgq.service.ToolsService;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("logic/tools")
@CrossOrigin
public class ToolsController {

    @Autowired
    private ToolsService toolsService;
    @Autowired
    private MinioClient minioClient;

    @RequestMapping(value = "select/regions/{pid}", method = RequestMethod.GET)
    @Operation(summary = "根据父级id查询子级地区")
    public SaResult buildRegionList(@PathVariable("pid")Integer pid) {

        return SaResult.ok().setData(toolsService.findByRegionPid(pid));
    }

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @Operation(summary = "文件上传")
    public SaResult fileUpload(MultipartFile file, Integer itemId) throws IOException, ServerException, InsufficientDataException,
            ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException,
            XmlParserException, InternalException {
        String fileName = "pic/" + System.currentTimeMillis() + "-" + file.getOriginalFilename();

        PutObjectArgs args = PutObjectArgs.builder()
                .bucket("team08")
                .object(fileName)
                .contentType(file.getContentType())
                .stream(file.getInputStream(), file.getSize(), -1)
                .build();
        minioClient.putObject(args);
//        toolsService.setPicUrl(fileName,itemId);
        return SaResult.ok().setMsg(fileName);
    }

    @RequestMapping(value = "download",method = RequestMethod.GET)
    @Operation(summary = "文件下载")
    public SaResult downloadFile(String fileName, String path, HttpServletResponse response) throws
            ServerException, IOException, InsufficientDataException, ErrorResponseException,
            NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
            InternalException {
        byte[] buffer = new byte[1024];
        int bytesRead;
        OutputStream out = null;

        GetObjectResponse objRes = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket("team08")
                        .object(path + "/" + fileName).build());
        String contentType = objRes.headers().get("Content-Type");
        response.setContentType(contentType + "; charset=UTF-8");

        out = response.getOutputStream();
        while ((bytesRead = objRes.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        objRes.close();
        out.close();

        return new SaResult().setCode(200).setMsg("下载成功");
    }
}
